/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.masterdata.calendar.dao.hibernate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import com.erp.masterdata.calendar.dao.MdFinanceCalendarPeriodDao;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendarPeriod;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendarPeriodCO;

@Repository
public class MdFinanceCalendarPeriodDaoImpl implements MdFinanceCalendarPeriodDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(MdFinanceCalendarPeriod obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(MdFinanceCalendarPeriod obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(MdFinanceCalendarPeriod obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(MdFinanceCalendarPeriod obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<MdFinanceCalendarPeriod> getDataObjects() {
        return this.daoSupport.getDataAllObject(MdFinanceCalendarPeriod.class);
    }

    @Override
    public MdFinanceCalendarPeriod getDataObject(int id) {
        return (MdFinanceCalendarPeriod)this.daoSupport.getDataObject(MdFinanceCalendarPeriod.class, id);
    }
    
    @Override
    public MdFinanceCalendarPeriod getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<MdFinanceCalendarPeriod> getDataObjects(MdFinanceCalendarPeriodCO paramObj) {
        String sql = "select c.* from md_finance_calendar_period c where calendar_code=:code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", paramObj.getCalendarCode());
        
        sql = sql + " order by c.period_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", MdFinanceCalendarPeriod.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public List<MdFinanceCalendarPeriod> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<MdFinanceCalendarPeriod> getDataObjects(Pages pages, MdFinanceCalendarPeriodCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, MdFinanceCalendarPeriodCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<MdFinanceCalendarPeriod> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, MdFinanceCalendarPeriodCO paramObj) {
        return null;
    }
    
    @Override
    public int getPeriodCountByCalendarCode(String calendarCode) {
        String sql = "select count(1) from md_finance_calendar_period c where c.calendar_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", calendarCode);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            return Integer.parseInt(String.valueOf(list.get(0)));
        }
        
        return 0;
    }
    
    @Override
    public List<MdFinanceCalendarPeriod> getPeriodListByCalendarCode(Pages pages, MdFinanceCalendarPeriodCO paramObj) {
        String sql = "select c.* from md_finance_calendar_period c where calendar_code=:code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", paramObj.getCalendarCode());
        
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "periodStatus", "and c.", args);
        
        sql = sql + " order by c.period_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", MdFinanceCalendarPeriod.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public void updateLineForVersion(String code) {
        String sql = "update md_finance_calendar_period set version = version+1 where calendar_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public boolean isClosedForPreviousPeriod(Integer periodId) {
        String sql = "select t.* from md_finance_calendar_period t "
                   + "where t.period_end_date<=(select p.period_start_date from md_finance_calendar_period p where p.period_id = :periodId)";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("periodId", periodId);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("t", MdFinanceCalendarPeriod.class);
        
        List<MdFinanceCalendarPeriod> list = this.daoSupport.selectDataSql(sql, entity, args);
        for(MdFinanceCalendarPeriod period: list) {
            if(period.getPeriodStatus().contentEquals("OPEN")) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public boolean isOpenedForPeriod(Integer periodId) {
        String sql = "select p.* from md_finance_calendar_period p where p.period_id = :periodId";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("periodId", periodId);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", MdFinanceCalendarPeriod.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list!=null&&list.size()>0) {
            MdFinanceCalendarPeriod period = (MdFinanceCalendarPeriod)list.get(0);
            if(period.getPeriodStatus().contentEquals("OPEN")) {
                return true;
            }
        }
        
        return false;
    }
    
    @Override
    public MdFinanceCalendarPeriod getPeriodNameByDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(date); 
        
        String sql = "select p.* from md_finance_calendar_period p where :dateStr between p.period_start_date and p.period_end_date";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("dateStr", dateStr);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", MdFinanceCalendarPeriod.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list!=null&&list.size()>0) {
            return (MdFinanceCalendarPeriod)list.get(0);
        }
        
        return null;
    }
    
    @Override
    public MdFinanceCalendarPeriod getPreviousPeriod(String periodName) {
        String sql = "select p.* from md_finance_calendar_period p "
                   + "where p.period_end_date < (select period_start_date from md_finance_calendar_period where period_name = :periodName) "
                   + "order by p.period_end_date desc limit 0,1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("periodName", periodName);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", MdFinanceCalendarPeriod.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list!=null&&list.size()>0) {
            return (MdFinanceCalendarPeriod)list.get(0);
        }
        
        return null;
    }
    
}
